<?php
/**
 * Zapp
 *
 * @version $Id: application.php 20 2011-06-05 14:50:31Z dm.barsukov@gmail.com $
 */

defined('ZAPP_ROOT_PATH')
    || define('ZAPP_ROOT_PATH', realpath(__DIR__ . '/../'));
defined('ZAPP_APPLICATION_PATH')
    || define('ZAPP_APPLICATION_PATH', ZAPP_ROOT_PATH . '/application');

// initialize application
require ZAPP_APPLICATION_PATH . '/Init.php';
require ZAPP_LIBRARY_PATH . '/Zapp/Application/Application.php';

try {
    $application = new \Zapp\Application\Application(ZAPP_ENVIRONMENT);
    $appOptions = $application->loadConfig(ZAPP_APPLICATION_PATH . '/configs/application.json');

    // localhost config
    if (is_readable(ZAPP_APPLICATION_PATH . '/configs/localhost.json')) {
        $temp = $application->loadConfig(ZAPP_APPLICATION_PATH . '/configs/localhost.json');
        $appOptions = $application->mergeOptions($appOptions, $temp);
    }

    $application->setOptions($appOptions)
                ->bootstrap()
                ->run();
} catch (Exception $exception) {
    echo "<html><title>Error</title><body><pre>\n"
       . "An error occured while starting the application.\n";
    if (ZAPP_ENVIRONMENT !== ZAPP_PRODUCTION) {
        echo $exception->getMessage() . "\n"
           . $exception->getTraceAsString() . "\n";
    }
    echo "</pre></body></html>";
    exit(1);
}
